package com.autonavi.yunda.yunji.controller;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.controller.request.UserRequest;
import com.autonavi.yunda.yunji.core.enums.OperationRecordType;
import com.autonavi.yunda.yunji.core.enums.RoleType;
import com.autonavi.yunda.yunji.core.model.condition.UserQueryCondition;
import com.autonavi.yunda.yunji.core.model.dto.OperationRecordDTO;
import com.autonavi.yunda.yunji.core.model.dto.PartUnitDTO;
import com.autonavi.yunda.yunji.core.model.dto.ProjectDTO;
import com.autonavi.yunda.yunji.core.model.mappers.OperationRecordMapper;
import com.autonavi.yunda.yunji.core.model.mappers.PartUnitMapper;
import com.autonavi.yunda.yunji.core.model.mappers.ProjectMapper;
import com.autonavi.yunda.yunji.core.vo.response.UserPageVO;
import com.autonavi.yunda.yunji.service.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cizhong.wcs
 * @date 2023/9/5 16:31
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserProjectService userProjectService;
    @Resource
    private BucUserService bucUserService;
    @Resource
    private OperationRecordService operationRecordService;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private OperationRecordMapper operationRecordMapper;
    @Resource
    private PartUnitMapper partUnitMapper;
    @Resource
    private FactoryUserService factoryUserService;

    @PostMapping("list")
    public UserPageVO list(@Validated @RequestBody(required = false) UserQueryCondition condition) {
        String workNo = bucUserService.getCurEmployeeId();
        RoleType userRole = userService.getUserRole(workNo);
        if (!RoleType.SUPER_ADMIN.equals(userRole) && !factoryUserService.checkPermission(workNo, "yunpro.api.user.manage")) {
            throw new AmapException(AmapExceptionCode.NO_PERMISSION_ERROR, "无权限");
        }
        return userService.listPage(condition);

    }

    @GetMapping("add")
    public void add(@RequestParam String userId) {
        userService.addUser(userId);
    }

    @PostMapping("update")
    public String update(@RequestBody UserRequest request) {
        String workNo = bucUserService.getCurEmployeeId();
        RoleType userRole = userService.getUserRole(workNo);
        if (!RoleType.SUPER_ADMIN.equals(userRole) && !factoryUserService.checkPermission(workNo, "yunpro.api.user.manage")) {
            throw new AmapException(AmapExceptionCode.NO_PERMISSION_ERROR, "无权限");
        }
        userService.fetchUserByUserIdOrThrow(request.getUserId());
        userService.updateUser(request.toUpdateDTO());
        if (request.getProjectIds() != null) {
            userProjectService.updateUserProjectIds(request.getUserId(), request.getProjectIds(), workNo);
        }
        operationRecordService.add(OperationRecordType.UserSubType.UPDATE, workNo);
        return "success";
    }

    /**
     * todo 刷新所有bdId，后续完全对接工厂权限后删除
     */
    @GetMapping("refreshAllBdId")
    public void refreshAllBdId(@RequestParam Long bdId) {
        projectMapper.update(null, new UpdateWrapper<ProjectDTO>().set("bd_id", bdId));
        operationRecordMapper.update(null, new UpdateWrapper<OperationRecordDTO>().set("bd_id", bdId));
        partUnitMapper.update(null, new UpdateWrapper<PartUnitDTO>().set("bd_id", bdId));
    }

    @GetMapping("invalidUserInfoCache")
    public void invalidUserInfoCache() {
        factoryUserService.invalidUserInfoCache();
    }
}
